<?php

namespace app\admin\model;

use think\Model;

class AdminMenu extends Model {

    protected $resultSetType = 'collection';

    /**
     * 获取左侧菜单列表 tip:先读缓存，没有读数据库，并生成缓存
     * @return type
     */
    public function getLeftMenu() {
        $data = cache('leftMenu');
        if ($data) {
            return $data;
        }
        $this->setLeftMenuCache();
        return cache('leftMenu');
    }

    /**
     * 全部菜单
     * @return type
     */
    public function getAllMenu() {
        $data = cache('menuCache');
        if ($data) {
            return $data;
        }
        $this->setAllMenuCache();
        return cache('menuCache');
    }

    /**
     * 生成左则菜单缓存
     */
    public function setLeftMenuCache() {
        $this->_reset();
        $_top = $this->where(['parentid' => 0, 'display' => 1, 'is_del' => 0])->order('list_order', 'asc')->select()->toArray();
        $top = array_column($_top, null, 'id');

        foreach ($top as $kf => &$_t) {
            $child = $this->where(['parentid' => $kf, 'display' => 1, 'is_del' => 0])->order('list_order', 'asc')->select()->toArray();
            if ($child) {
                $child = array_column($child, null, 'id');
                foreach ($child as $ks => &$_s) {
                    $threes = $this->where(['parentid' => $ks, 'display' => 1, 'is_del' => 0])->order('list_order', 'asc')->select()->toArray();
                    if ($threes) {
                        $_s['child'] = array_column($threes, null, 'id');
                    } else {
                        $_s['child'] = $threes;
                    }
                }
            }
            $_t['child'] = $child;
        }
        cache('leftMenu', null);
        return cache('leftMenu', $top);
    }

    /**
     * 生成全部菜单缓存
     */
    public function setAllMenuCache() {
        $this->_reset();
        $_top = $this->where(['parentid' => 0, 'is_del' => 0])->order('list_order', 'asc')->select()->toArray();
        $top = array_column($_top, null, 'id');

        foreach ($top as $kf => &$_t) {
            $child = $this->where(['parentid' => $kf, 'is_del' => 0])->order('list_order', 'asc')->select()->toArray();
            if ($child) {
                $child = array_column($child, null, 'id');
                foreach ($child as $ks => &$_s) {
                    $threes = $this->where(['parentid' => $ks, 'is_del' => 0])->order('list_order', 'asc')->select()->toArray();
                    if ($threes) {
                        $_s['child'] = array_column($threes, null, 'id');
                    } else {
                        $_s['child'] = $threes;
                    }
                }
            }
            $_t['child'] = $child;
        }
        cache('menuCache', null);
        return cache('menuCache', $top);
    }

    public function _reset() {
        $table = $this->getTable();
        //设置级别
        //初始化
        $this->execute("update  {$table} set `level`=0");
        //设置一级值
        $this->execute("update  {$table} set `level`=1 where parentid=0");
        //设置二级值
        $this->execute("update  {$table} as m1,{$table} as m2 set m1.`level`=2 where m1.parentid = m2.id and m2.parentid=0");
        //设置三级值
        $this->execute("update  {$table} as m1,{$table} as m2 set m1.`level`=3 where m1.parentid = m2.id and m2.`level`=2");
        //设置四级值
        $this->execute("update  {$table} as m1,{$table} as m2 set m1.`level`=4 where m1.parentid = m2.id and m2.`level`=3");
        //设置五级值
        $this->execute("update  {$table} as m1,{$table} as m2 set m1.`level`=5 where m1.parentid = m2.id and m2.`level`=4");

        //设置上级目录
        //初始化
        $this->execute("update  {$table} set parentids = ''");
        //设置一级
        $this->execute("update  {$table} set parentids = 0 WHERE parentid=0");
        //设置二级
        $this->execute("update  {$table} as m1,{$table} as m2 set m1.`parentids` = concat(m2.parentids,',', m1.parentid) where m1.parentid = m2.id and m2.parentid=0");
        //设置三级
        $this->execute("update  {$table} as m1,{$table} as m2 set m1.`parentids` = concat(m2.parentids,',', m1.parentid) where m1.parentid = m2.id and m2.`level`=2");
        //设置四级
        $this->execute("update  {$table} as m1,{$table} as m2 set m1.`parentids` = concat(m2.parentids,',', m1.parentid) where m1.parentid = m2.id and m2.`level`=3");
    }

}
